﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using CoolERP_Common;

namespace CoolERP_Web.User
{
    [PermissionCheck(false)]
    public partial class KCMX : BasePage
    {
        private Guid WPID
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }
        private string WPLX
        {
            get { return this.GetUrlParam("type") == null ? string.Empty : this.GetUrlParam("type").ToString(); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        #region=======GridView处理=======
        private void BindData()
        {
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("WPID", WPID.ToString());
            using (KCBLL kBLL = new KCBLL())
            {
                int RecordCount = 0;
                List<KCMXModel> itemLists = kBLL.GetKCMX(dc, "CKID", null, 100, out RecordCount);
                GridView1.DataSource = itemLists;
                GridView1.DataBind();
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                KCMXModel entity = e.Row.DataItem as KCMXModel;
                DropDownList ddlCK = e.Row.FindControl("ddlCK") as DropDownList;
                if (ddlCK != null)
                {
                    BindCK(ddlCK, "");
                    if (ddlCK.Items.FindByValue(entity.CKID.ToString()) != null)
                        ddlCK.SelectedValue = entity.CKID.ToString();
                }
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList ddlCK = e.Row.FindControl("ddlCK") as DropDownList;
                if (ddlCK != null)
                    BindCK(ddlCK, "");
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            BindData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //Guid id = new Guid(GridView1.DataKeys[e.RowIndex].Value.ToString());
            //DropDownList ddlCK = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlCK");
            //TextBox txtQU = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtQU");
            //TextBox txtHANG = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtHANG");
            //TextBox txtLIE = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtLIE");
            //TextBox txtCENG = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtCENG");
            //TextBox txtGE = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtGE");
            //TextBox txtSL = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSL");
            //using (KCBLL kBLL = new KCBLL())
            //{
            //    KC item = kBLL.GetEntity(t => t.ID == id);
            //    item.CKID = new Guid(ddlCK.SelectedValue);
            //    item.SL = string.IsNullOrEmpty(txtSL.Text.Trim()) ? 0 : Convert.ToDouble(txtSL.Text.Trim());
            //    kBLL.Update(item);
            //}
            //GridView1.EditIndex = -1;
            //BindData();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            using (KCBLL kBLL = new KCBLL())
            {
                KC item = kBLL.GetEntity(t => t.ID == new Guid(GridView1.DataKeys[e.RowIndex].Value.ToString()));
                if (item.SL > 0)
                    ShowMsg("删除失败！库存还有物料");
                else
                {
                    kBLL.Deletes("ID", GridView1.DataKeys[e.RowIndex].Value.ToString());
                    GridView1.EditIndex = -1;
                    BindData();
                }
            }
        }
        #endregion

        #region=======添加类型=======
        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = true;
            BindData();
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            DropDownList ddlCK = (DropDownList)GridView1.FooterRow.FindControl("ddlCK");
            TextBox txtQU = (TextBox)GridView1.FooterRow.FindControl("txtQU");
            TextBox txtHANG = (TextBox)GridView1.FooterRow.FindControl("txtHANG");
            TextBox txtLIE = (TextBox)GridView1.FooterRow.FindControl("txtLIE");
            TextBox txtCENG = (TextBox)GridView1.FooterRow.FindControl("txtCENG");
            TextBox txtGE = (TextBox)GridView1.FooterRow.FindControl("txtGE");
            KC tempEntity = new KC();
            using (KCBLL kBLL = new KCBLL())
            {
                tempEntity = kBLL.GetList(t => t.WPID == new Guid(Request.QueryString["id"])).FirstOrDefault();
                //仓位信息
                ZL_CW item = new ZL_CW();
                item.CKID = new Guid(ddlCK.SelectedValue);
                item.QU = txtQU.Text.Trim().PadLeft(2, '0');
                item.HANG = txtHANG.Text.Trim().PadLeft(3, '0');
                item.LIE = txtLIE.Text.Trim().PadLeft(3, '0');
                item.CENG = txtCENG.Text.Trim().PadLeft(3, '0');
                item.GE = txtGE.Text.Trim().PadLeft(3, '0');
                item.BH = string.Format("{0}-{1}-{2}-{3}-{4}", item.QU, item.HANG, item.LIE, item.CENG, item.GE);
                //库存信息
                KC entity = new KC();
                entity.ID = Guid.NewGuid();
                entity.CKID = new Guid(ddlCK.SelectedValue);
                entity.WPID = new Guid(Request.QueryString["id"]);
                entity.WPLX = EnumHelper.GetDescriptionByName(typeof(EnumKC_LX), WPLX);
                entity.SL = 0;
                entity.DW = tempEntity.DW;
                entity.PJDJ = tempEntity.PJDJ;
                entity.ZXDJ = tempEntity.ZXDJ;
                entity.ZL_CW = item;
                if (kBLL.Insert(entity))
                    ShowMsg("添加成功");
                else
                    ShowMsg("添加失败");
            }
        }

        protected void btnCancel_Click(object sender, ImageClickEventArgs e)
        {
            GridView1.ShowFooter = false;
            BindData();
        }
        #endregion
    }
}